<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\service\RuleService;
use think\facade\Db;
use think\facade\View;
use think\facade\Session;
use app\common\Error;

class Base extends BaseController
{
    protected $middleware = ['check_admin_login', 'check_admin_auth'];
    // protected $middleware = ['check_admin_login'];

    public $menu;
    private $service;

    public function __construct()
    {
        $this->service = new RuleService();
        self::getMenuData();
    }

    // 获取菜单数据
    public function getMenuData()
    {
        $data = $this->service->getMenuData();
        $this->menu = $data;
        View::assign('menu_data', $data);
    }

    // 获取菜单数据
    public function log($videoid,$content)
    {
        Db::name('actionlog')->insert(['local_id' => Session::get('user_id'),'video_id'=>$videoid, 'content' => $content, 'created_at' => time()]);
    }

    public function postApiJson($api, $param, $header = ['Content-Type: application/json']) {


        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $api,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($param),
            CURLOPT_HTTPHEADER => $header,
        ));
        $response = curl_exec($curl);
        curl_close($curl);

        return json_decode($response, true);
    }

    public function postApiform($api, $param, $header = ['Content-Type: application/json']) {


        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $api,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => ($param),
            CURLOPT_HTTPHEADER => $header,
        ));
        $response = curl_exec($curl);
        curl_close($curl);

        return json_decode($response, true);
    }
    public function getApi($api, $param, $header = ['Content-Type: application/json']) {

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $api . '?' . http_build_query($param),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => $header,
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response, true);
    }
}